<?php

namespace app\admin\controller;

use app\common\Helper;
use app\admin\model\Askpricerecord as AskpricerecordMdl;

use think\Db;
use think\Exception;
use think\Request;

/**
 * 业主
 */
class Askpricerecord extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new AskpricerecordMdl();
    }

    /**
     * 列表
     */
    public function getList(){
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        //$defaultWhere = $this->getDefaultWhere();
        $defaultWhere = [];
        $where = $defaultWhere;
        if (isset($param->name)) {
            $where['name'] = ['like', '%' . $param->name . '%'];
        }
        $model = $this->model;
        $order = 'createDate DESC';
        $result = $model->where($where)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();

        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关业主');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 详情
     */
    public function detail(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能这空');
        }

        $id = $param->id;
        $where = [
            'id' => $id
        ];
        $fields = '*';
        $item = $model->where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关业主');
        } else {
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }

        $id = $param->id;
        Db::startTrans();
        try {
            $result = $model->where('id', $id)->delete();
            if($result !== false){
                // Query执行后的操作
                $model->_after_delete($id);

                Db::commit();
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '删除成功');
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $e->getMessage());
        }
    }

}